export function getCurrentDateStr() {
	const now = new Date();
	var currentTime = now.getFullYear() + '-' +
		('0' + (now.getMonth() + 1)).slice(-2) + '-' +
		('0' + now.getDate()).slice(-2) + '-' +
		('0' + now.getHours()).slice(-2) + ':' +
		('0' + now.getMinutes()).slice(-2) + ':' +
		('0' + now.getSeconds()).slice(-2);
	return currentTime
}

export function chooseImage() {
	return new Promise((resolve, reject) => {

		uni.chooseImage({
			// count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], // 从相册选择
			success: (res) => {
				const tempFilePaths = res.tempFilePaths;
				resolve(tempFilePaths[0]);
			},
			fail: (error) => {
				reject(error);
			}
		});
	});
}

export function batch_chooseImage() {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			// count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], // 从相册选择
			success: (res) => {
				const tempFilePaths = res.tempFilePaths;
				resolve(tempFilePaths);
			},
			fail: (error) => {
				reject(error);
			}
		});
	});
}

export function uploadImage(filePath) {
	console.log('执行上传图片===', filePath);
	return new Promise((resolve, reject) => {
		const dateStr = this.getCurrentDateStr() + '.png';
		uni.showLoading({
			title: '图片上传中...'
		});
		uniCloud.uploadFile({
			filePath: filePath,
			cloudPath: dateStr,
			onUploadProgress: function(progressEvent) {
				console.log('长传图进度', progressEvent);
				var percentCompleted = Math.round(
					(progressEvent.loaded * 100) / progressEvent.total
				);
			},
			success(res) {
				console.log('上传成功', res);
				if (res.success == true) {
					var imageUrl = res.fileID;
					resolve(imageUrl);
				} else {
					reject('上传失败');
				}

			},
			fail(err) {
				uni.hideLoading();
				reject('上传失败');
				console.error('上传失败', err);
				uni.showToast({
					title: err.toString(),
					icon: 'none'
				})
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}